package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.MaterielClass;
import com.zyd.shiro.business.service.PakMaterielClassService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakMaterielClass;
import com.zyd.shiro.persistence.mapper.PakMaterielClassMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakMaterielClassServiceImpl implements PakMaterielClassService {

    @Autowired
    private PakMaterielClassMapper pakMaterielClassMapper;

    @Override
    public MaterielClass insert(MaterielClass materielClass) {
        Assert.notNull(materielClass, "对象不可为空！");
        materielClass.getPakMaterielClass().setUpdateTime(new Date());
        materielClass.getPakMaterielClass().setCreateTime(new Date());
        pakMaterielClassMapper.insertSelective(materielClass.getPakMaterielClass());
        return materielClass;
    }

    @Override
    public void insertList(List<MaterielClass> materielClasss) {

    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakMaterielClassMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(MaterielClass materielClass) {
        Assert.notNull(materielClass, "对象不可为空！");
        materielClass.setUpdateTime(new Date());
        return pakMaterielClassMapper.updateByPrimaryKey(materielClass.getPakMaterielClass()) > 0;
    }

    @Override
    public boolean updateSelective(MaterielClass materielClass) {
        Assert.notNull(materielClass, "对象不可为空！");
        materielClass.setUpdateTime(new Date());
        return pakMaterielClassMapper.updateByPrimaryKeySelective(materielClass.getPakMaterielClass()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public MaterielClass getByPrimaryKey(Long primaryKey) {
        PakMaterielClass pakMaterielClass = pakMaterielClassMapper.selectByPrimaryKey(primaryKey);
        return null == pakMaterielClass ? null : new MaterielClass(pakMaterielClass);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public MaterielClass getOneByEntity(MaterielClass test) {
        Assert.notNull(test, "对象不可为空！");
        PakMaterielClass pakMaterielClass = pakMaterielClassMapper.selectOne(test.getPakMaterielClass());
        return null == pakMaterielClass ? null : new MaterielClass(pakMaterielClass);
    }

    @Override
    public List<MaterielClass> listAll() {
        List<PakMaterielClass> pakMaterielClasss = pakMaterielClassMapper.selectAll();
        if (CollectionUtils.isEmpty(pakMaterielClasss)) {
            return null;
        }
        List<MaterielClass> materielClasss = new ArrayList<>();
        for (PakMaterielClass pakMaterielClass : pakMaterielClasss) {
            materielClasss.add(new MaterielClass(pakMaterielClass));
        }
        return materielClasss;
    }

    @Override
    public List<MaterielClass> listByEntity(MaterielClass entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<MaterielClass> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakMaterielClass> pakMaterielClasss = pakMaterielClassMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakMaterielClasss)) {
            return null;
        }
        List<MaterielClass> materielClasss = new ArrayList<>();
        for (PakMaterielClass pakMaterielClass : pakMaterielClasss) {
            materielClasss.add(new MaterielClass(pakMaterielClass));
        }
        PageInfo bean = new PageInfo<PakMaterielClass>(pakMaterielClasss);
        bean.setList(materielClasss);
        return bean;
    }
@Override
    public void increase(PakMaterielClass pakMaterielClass) {
        pakMaterielClassMapper.insert(pakMaterielClass);
    }




}
